package com.example.test.conview.view.core;

import static com.puty.conputyview.common.PutyConst.HOUSEHOLD_SERIES;
import static com.puty.conputyview.common.PutyConst.INDUSTRY_SERIES;
import static com.puty.conputyview.common.PutyConst.SERIES_TYPE;
import static com.puty.conputyview.common.PutyConst.TEXT;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.puty.conputyutils.BitmapUtils;
import com.puty.conputyutils.LogUtils;
import com.puty.conputyutils.SharedPreferencesHelperUtils;
import com.puty.conputyutils.StringUtils;
import com.puty.conputyview.R;
import com.puty.conputyview.common.PutyConst;
import com.puty.conputyview.view.DrawArea;
import com.puty.conputyview.view.tool.Global;

import java.util.ArrayList;

/**
 * Text文字处理
 */
public class TextElement extends Element {
    private final static  String TAG = "TextElement";

    public int fontColor = Color.BLACK;
    public Bitmap tempImg;
    public Bitmap img;
    /**
     * 自动换行 1 换行 0 不换行
     */
    public int autoBreak = 0;

    /**
     * 行间距 0=1.0 单倍行距 ,1=1.2倍行距,2=1.5倍行距,3=两倍行距,4=自定义
     */
    public int textRowMode = 0;

    private TextView tvElementSketchpad;

    /**
     * 待画内容列表
     */
    String[] drawTextList;

    public TextElement(Context context, String content, float width, float height, Label lb) {
        super(context, lb);
        super.type = TEXT;
        super.content = content;
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            initHousehold();
        } else if (SERIES_TYPE == INDUSTRY_SERIES) {
            initIndustry(width, height);
        }
    }


    /**
     * 家用系列初始化数据
     */
    private void initHousehold() {
        super.orientation = lb.printInfo.PrintDirect;
        if (orientation == 0) {
            super.width = (content.length() + 0.5f) * Global.fontSize[fontIndex] * 8 * lb.scale;
        } else {
            super.height = (content.length() + 0.5f) * Global.fontSize[fontIndex] * 8 * lb.scale;
        }
        setFontSize();
        View elementTextYY = LayoutInflater.from(super.mContext).inflate(R.layout.element_text_yy, null);
        tvElementSketchpad = elementTextYY.findViewById(R.id.tv_text);
    }

    /**
     * 工业系列初始化数据
     */
    private void initIndustry(float width, float height) {
        this.fontIndex = getFontIndex(2, lb.Width, lb.Height);
        if (width > 0) {
            super.width = width;
        } else {
            super.width = (content.length() + 0.5f) * Global.fontSize[fontIndex] * 8 * lb.scale;
        }
        super.height = height;
        setFontSize();
    }

    @Override
    public void setFontSize() {
        LogUtils.i(TAG, "TextElement fontIndex:" + fontIndex);
        //字体大小
        float fz = Global.fontSize[Math.abs(fontIndex)] * 8 * lb.scale;
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            if (DrawArea.dragView != null) {
                // 微调字体大小。
                // 生成打印图片时等比例缩放字体，字体大小会丢失，所以在这里处理，保证压缩字体时，字体大小是整数
                float fontRadio = 1.0f;
                float labelHeight = lb.labelHeight;
                int theme = (int) SharedPreferencesHelperUtils.getSharedPreference("theme", R
                        .style.AppTheme);
                if (theme == R.style.YYTheme) {
                    if (labelHeight < 9) {
                        fontRadio = supplement8Number((int) (6.0f * 7.09f)) / (9.f * 8.f * lb.scale);
                    } else if (labelHeight == 9) {
                        fontRadio = supplement8Number((int) (9.0f * 7.09f)) / (9.f * 8.f * lb.scale);
                    } else {
                        fontRadio = supplement8Number((int) (9.0f * 7.09f)) / (7.f * 8.f * lb.scale);
                    }
                } else { // YY
                    fontRadio = supplement8Number((int) (9.0f * 7.09f)) / (9.f * 8.f * lb.scale);
                }

                double fontSize = Math.floor(fz * fontRadio);
                fz = (float) (fontSize / fontRadio);
            }
        }
        this.fontSize = fz;
    }

    /**
     * 宽度不是8的倍数，需要转成8的倍数，否则生成黑白图片的时候，会舍弃掉一部分
     *
     * @return
     */
    private float supplement8Number(int number) {
        if (number % 8 != 0) {
            return (8 - number % 8) + number + 0.0f;
        } else {
            return number + 0.0f;
        }
    }

    @Override
    public void init() {
        super.init();
        if (SERIES_TYPE == HOUSEHOLD_SERIES && TextUtils.isEmpty(this.content)) {
            this.content = " ";
            //this._content = _context.getString(R.string.text);
        }
        this.initBitmap();
        this.img = this.tempImg;
        rate(rate);
    }

    /**
     * 生成图片
     */
    public void initBitmap() {
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            householdBitmap();
        } else if (SERIES_TYPE == INDUSTRY_SERIES) {
            industryBitmap();
        }
    }

    /**
     * 家用系列生成图片
     */
    private void householdBitmap() {
        //字体
//        Typeface typeface = Typeface.createFromAsset(_context.getAssets(), "fonts/点阵细明体.ttf");
        tvElementSketchpad.setTypeface(typeface);
        tvElementSketchpad.getPaint().setAntiAlias(true); // 设置抗锯齿
        //设置字体大小
        tvElementSketchpad.getPaint().setTextSize(this.fontSize);
        tvElementSketchpad.getPaint().setFakeBoldText(fontBlod == 0 ? false : true);
        tvElementSketchpad.getPaint().setTextSkewX(fontItalic == 0 ? 0 : -0.25f);
        tvElementSketchpad.getPaint().setUnderlineText(fontUnderline == 0 ? false : true);
        tvElementSketchpad.getPaint().setStrikeThruText(fontDelete == 0 ? false : true);
        tvElementSketchpad.getPaint().setColor(fontColor);
        tvElementSketchpad.getPaint().setStrokeCap(Paint.Cap.ROUND);
        tvElementSketchpad.getPaint().setStrokeJoin(Paint.Join.ROUND);
        tvElementSketchpad.getPaint().setLinearText(true);
        tvElementSketchpad.setIncludeFontPadding(false);
        tvElementSketchpad.setText(content);
//        tvElementSketchpad.setEms(orientation == 0 ? 0 : 1);
        String[] lines = content.split("\r\n|\r|\n", -1);
        if (orientation == 0) {
            // 第一种获取宽度 ，只能获取单行宽度
            float maxWidth = 0;
            for (String item : lines) {
                float w = getContentWhidth(tvElementSketchpad.getPaint(), item);
                if (fontItalic == 1) { // 斜体需要设置右边距，不然文本内容会超显示范围，被截取
                    float cellHeight = tvElementSketchpad.getPaint().descent() - tvElementSketchpad.getPaint().ascent();
                    float tailIndent = 25.f * cellHeight / 180.f; // 这里不理解为什么这么算，参考iOS的
                    w += tailIndent;
                }

                if (maxWidth < w) {
                    maxWidth = w;
                }
            }
            this.width = maxWidth;
            BitmapUtils.layoutView(tvElementSketchpad, (int) width, View.MeasureSpec.makeMeasureSpec((int) height, View.MeasureSpec.EXACTLY));
        } else {
            float maxHeight = 0;
            for (String item : lines) {
                float h = (tvElementSketchpad.getPaint().descent() - tvElementSketchpad.getPaint().ascent()) * item.length();
                if (maxHeight < h) {
                    maxHeight = h;
                }
            }
            this.height = maxHeight;
            BitmapUtils.layoutView(tvElementSketchpad, View.MeasureSpec.makeMeasureSpec((int) width, View.MeasureSpec.EXACTLY), (int) height);
        }
    }

    /**
     * 工业系列生成图片
     */
    private void industryBitmap() {
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        //字体
        if (typeface != null) {
            textPaint.setTypeface(typeface);
        }
        //设置字体大小
        textPaint.setTextSize(this.fontSize);
        LogUtils.i(TAG,"set fontSize:"+this.fontSize);

        textPaint.setFakeBoldText(fontBlod == 0 ? false : true);
        textPaint.setTextSkewX(fontItalic == 0 ? 0 : -0.25f);
        textPaint.setUnderlineText(fontUnderline == 0 ? false : true);
        textPaint.setStrikeThruText(fontDelete == 0 ? false : true);
        textPaint.setColor(fontColor);
        textPaint.setStrokeCap(Paint.Cap.ROUND);
        textPaint.setStrokeJoin(Paint.Join.ROUND);
        textPaint.setLinearText(true);

        if (this.rate == 0 || this.rate == 180) {
            this.height = getTextReelHeight(textPaint);//预估文本的实际高度
        } else {
            this.width = getTextReelHeight(textPaint);//预估文本的实际高度
        }
        Bitmap bmp;
        float w = this.width;
        float h = this.height;
        if (this.rate == 90 || this.rate == 270) {
            float temp = 0;
            temp = w;
            w = h;
            h = temp;
        }
        if (w <= 0) {
            w = 1;
        }
        if (h <= 0) {
            h = 1;
        }
        bmp = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bmp);

        //获取字体高度
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;

        float left = 0;
        float top = 0;
        String[] arraysStrings = this.drawTextList;
        for (int k = 0; k < arraysStrings.length; k++) {
            if (this.autoBreak == 1) {//换行
                if (k > 0) {
                    top += textHeight;//换行
                    top += this.textRowSpace*8*lb.scale;//加上行距
                }
            }

            switch (textMode) {
                case 1:
                    textalignCenter(left, top, textHeight, arraysStrings[k], textPaint, canvas, isAntiWhite,foreground);//反白
                    break;
                case 2:
                    textalignRight(left, top, textHeight, arraysStrings[k], textPaint, canvas, isAntiWhite,foreground);//反白
                    break;
                case 3://拉伸
                    int padding =  this.fontItalic == 1 ? 8 : 0;
                    float space = 0;
                    int index = k;
                    if(arraysStrings.length>1 && k == arraysStrings.length - 1){
                        index = arraysStrings.length-2;
                    }
                    int len =  arraysStrings[index].length();
                    //获取总的长度
                    float total = getContentWhidth(textPaint,arraysStrings[index]);
                    float surplusWidth = w - total - textCellSpace*8*lb.scale * (len - 1) - padding;
                    space = surplusWidth / (len - 1);//获取字符空隙 总宽度-字符串宽度
                    textalignPP(left - (this.fontItalic == 1 ? 4 : 0), top, textHeight, arraysStrings[k], textPaint, canvas, space, isAntiWhite,foreground);
                    break;
                default:
                    textalignLeft(left, top, textHeight, arraysStrings[k], textPaint, canvas, isAntiWhite,foreground);
                    break;
            }
        }
        this.tempImg = bmp;
    }

    /**
     * 预估文本高度
     */
    private float getTextReelHeight(Paint textPaint) {
        float w = this.width;
        float h = this.height;
        if (this.rate == 90 || this.rate == 270) {
            float temp = w;
            w = h;
            h = temp;
        }
        //获取字体高度
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;

        float left = 0;
        float top = 0;
        String result = "";
        String[] arraysStrings = content.split("\n");
        for (int k = 0; k < arraysStrings.length; k++) {
            //换行
            if (this.autoBreak == 1) {
                if (k > 0 && !TextUtils.isEmpty(result)) {
                    result += "\n";
                    top += textHeight;//换行
                    top += this.textRowSpace*8*lb.scale;//加上行距
                }
            }
            char[] chars = arraysStrings[k].toCharArray();
            if (isArab || StringUtils.textContainsKhmer(content)) {
                result += chars[0];
                int start = 0;
                for (int i = 1; i < arraysStrings[k].length(); i++) {
                    String vString = arraysStrings[k].substring(start, i);
                    //单个字体宽度
                    float actulWidth = getContentWhidth(textPaint,vString);//计算字符串宽度
                    float singleCharacterWidth = getContentWhidth(textPaint,"ش");//计算最宽的字符的宽度
                    if (actulWidth > w - singleCharacterWidth&&!TextUtils.isEmpty(result)) {//去掉一个字符长度
                        top += textHeight;//换行
                        top += this.textRowSpace*8*lb.scale;//加上行距
                        result += "\n";
                        start = i;
                    }
                    result += chars[i];
                }
            } else {
                char[] arr = arraysStrings[k].toCharArray();
                int len = arr.length;
                for (int i = 0; i < len; i++) {
                    String vString = String.valueOf(arr[i]);
                    //单个字体宽度
                    float actulWidth = getContentWhidth(textPaint,vString);//计算字符实际宽度
                    //判断是否自动换行
                    if (this.autoBreak == 1) {
                        //判断是否超出宽度，超出，换行
                        if (left + actulWidth + textCellSpace*8*lb.scale > w&& !TextUtils.isEmpty(result)) {
                            left = 0;
                            top += textHeight;//换行
                            top += this.textRowSpace*8*lb.scale;//加上行距
                            result += "\n";
                        }
                    }
                    left += actulWidth + textCellSpace*8*lb.scale;
                    result += vString;//拼接字符串
                }
            }
            left = 0;
        }
        if(this.rate == 180){//旋转180度的时候换行往上走
            String[] split = result.split("\n");
            if(drawTextList != null && split!=null){
                this.top += textHeight*(drawTextList.length - split.length);//换行
                this.top += this.textRowSpace*8*lb.scale*(drawTextList.length - split.length);//加上行距
            }
            drawTextList = split;
        }else {
            drawTextList = result.split("\n");
        }
        //得到字体的实际高度
        return top + textHeight;
    }

    @Override
    public void zoom(float x, float y, float width, float height) {
        super.zoom(x, y, width, height);
        switch (rate) {
            case PutyConst.RATE_0:
                this.width += x;
                if (this.width <= PutyConst.RATE_BOUNDARY) {
                    super.width = PutyConst.RATE_BOUNDARY;
                } else if (this.left + this.width + PutyConst.RATE_INCREASE >= width) {
                    this.width -= x;
                    return;
                }
                break;
            case PutyConst.RATE_180:
                this.width -= x;
                this.left += x;
                if (width <= PutyConst.RATE_BOUNDARY) {
                    this. width = PutyConst.RATE_BOUNDARY;
                } else if (this.left + this.width + PutyConst.RATE_INCREASE >= width) {
                    this.width -= x;
                    return;
                }
                break;
            case PutyConst.RATE_90:
                this.height += y;
                if (this.height <= PutyConst.RATE_BOUNDARY) {
                    this.height = PutyConst.RATE_BOUNDARY;
                } else if (this.top + this.height + PutyConst.RATE_INCREASE >= height) {
                    this.height -= y;
                    return;
                }
                break;
            case PutyConst.RATE_270:
                height -= y;
                this.top += y;
                if (this.height <= PutyConst.RATE_BOUNDARY) {
                    this.height = PutyConst.RATE_BOUNDARY;
                } else if (this.top + this.height + PutyConst.RATE_INCREASE >= height) {
                    this.height -= y;
                    return;
                }
                break;
        }

        this.rate(this.rate);
        zoomOtherSelected(x, y);
        //重新初始化
        this.initBitmap();
    }

    @Override
    public void rate(int degree) {
        this.img = this.tempImg;
        if (this.img == null) {
            LogUtils.e(TAG, "rate ---> img == null ");
            return;
        }
        Matrix matrix = new Matrix();
        try {
            matrix.setRotate(degree, this.img.getWidth() / 2, this.img.getHeight() / 2);
            this.img = Bitmap.createBitmap(this.img, 0, 0, this.img.getWidth(), this.img.getHeight(),
                    matrix, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            int tempDegree = degree;
            this.rate = tempDegree;
        } else if (SERIES_TYPE == INDUSTRY_SERIES) {
            rotation(degree);
        }

    }

    /**
     * 绘制
     *
     * @param canvas 画布对象
     */
    public void draw(Canvas canvas) {
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            householdDraw(canvas);
        } else if (SERIES_TYPE == INDUSTRY_SERIES) {
            industryDraw(canvas);
        }
    }

    /**
     * 家用绘制
     *
     * @param canvas 画布对象
     */
    private void householdDraw(Canvas canvas) {
        if (width == 0 || height == 0) return;
        Canvas textCanvas = new Canvas();
        Bitmap textBitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config
                .ARGB_8888);
        textCanvas.setBitmap(textBitmap);
        // 多行处理
        String[] lines = content.split("\r\n|\r|\n", -1);

        if (orientation == 0) {
            float childHeight = height / lines.length;
            tvElementSketchpad.getPaint().setTextAlign(Paint.Align.LEFT);
            for (int i = 0; i < lines.length; i++) {
                textCanvas.drawText(lines[i], 0, (float) (((i + 0.5) * childHeight) - (
                        tvElementSketchpad.getPaint().getFontMetrics().ascent + tvElementSketchpad
                                .getPaint().getFontMetrics().descent) / 2), tvElementSketchpad.getPaint());
            }
        } else {
            tvElementSketchpad.getPaint().setTextAlign(Paint.Align.CENTER);

            // 1、计算每行宽度
            ArrayList<Float> lineWidthList = new ArrayList<>();
            float totalWidth = 0;
            for (int i = 0; i < lines.length; i++) {
                String item = lines[i];
                float maxLineWidth = 0;
                for (int k = 0; k < item.length(); k++) {
                    String s = String.valueOf(item.charAt(k));
                    float w = tvElementSketchpad.getPaint().measureText(s);
                    if (maxLineWidth < w) {
                        maxLineWidth = w;
                    }
                }
                if (maxLineWidth == 0) {
                    if (TextUtils.isEmpty(content)) {
                        maxLineWidth = tvElementSketchpad.getPaint().measureText("一");
                    } else {
                        maxLineWidth = tvElementSketchpad.getPaint().measureText(String
                                .valueOf(content.charAt(0)));
                    }
                }
                totalWidth += maxLineWidth;
                lineWidthList.add(maxLineWidth);
            }

            // 2、绘制
            float x = 0;
            float childHeight = tvElementSketchpad.getPaint().descent() - tvElementSketchpad
                    .getPaint().ascent();
            for (int i = 0; i < lines.length; i++) {
                if (i == 0) { // 从右往左排
                    x = (width + totalWidth) / 2 - lineWidthList.get(i) / 2;
                } else {
                    x -= (lineWidthList.get(i) + lineWidthList.get(i - 1)) / 2;
                }

                String item = lines[i];
                char[] indexs = getKeyChar(item);
                int textCount = item.toCharArray().length;

                if (textCount > 0) {

                    Paint.FontMetrics fm = tvElementSketchpad.getPaint().getFontMetrics();

                    for (int k = 0; k < textCount; k++) {
                        textCanvas.drawText(
                                String.valueOf(indexs[k]), x
                                , (float) (((k + 0.5) * childHeight) - (fm.ascent + fm.descent) / 2)
                                ,
                                tvElementSketchpad.getPaint());
                    }
                }
            }
        }
        this.img = textBitmap;
        Rect sr = new Rect(0, 0, this.img.getWidth(), this.img.getHeight());
        canvas.drawBitmap(this.img, sr, new RectF(left, top, left + this.img
                .getWidth(), top + this.img.getHeight()), null);
        super.draw(canvas);
    }

    /**
     * 工业绘制
     * @param canvas 画布对象
     */
    private void industryDraw(Canvas canvas) {
        super.draw(canvas);
        try {
            //原始区域
            Rect sr = new Rect(0, 0, this.img.getWidth(), this.img.getHeight());

            canvas.drawBitmap(this.img, sr, new RectF(left, top, left + this.img
                    .getWidth(), top + this.img.getHeight()), null);
        }catch (Exception e) {
            LogUtils.e(TAG, "industryDraw Exception = " + e.toString());
        }
        if (isselected && isLock == 0) {
            //画缩放框
            //画右边缩放图标
            if (this.rate == PutyConst.RATE_0) {
                float hl = this.left + this.width - this.puhBitmap.getWidth() / 2;
                float ht = this.top + this.height / 2 - this.puhBitmap.getHeight() / 2;
                float hr = this.left + this.width + this.puhBitmap.getWidth() / 2;
                float hb = this.top + this.height / 2 + this.puhBitmap.getHeight() / 2;
                Rect rs = new Rect(0, 0, this.puhBitmap.getWidth(), this.puhBitmap.getHeight());
                RectF ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.puhBitmap, rs, ds, null);

                //左上角删除图标
                hl = this.left - this.deleteBitmap.getWidth() / 2;
                ht = this.top - this.deleteBitmap.getHeight() / 2;
                hr = this.left + this.deleteBitmap.getWidth() / 2;
                hb = this.top + this.deleteBitmap.getHeight() / 2;
                rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
                ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.deleteBitmap, rs, ds, null);

            } else if (this.rate == PutyConst.RATE_180) {
                float hl = this.left - this.puhBitmap.getWidth() / 2;
                float ht = this.top + this.height / 2 - this.puhBitmap.getHeight() / 2;
                float hr = this.left + this.puhBitmap.getWidth() / 2;
                float hb = this.top + this.height / 2 + this.puhBitmap.getHeight() / 2;
                Rect rs = new Rect(0, 0, this.puhBitmap.getWidth(), this.puhBitmap.getHeight());
                RectF ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.puhBitmap, rs, ds, null);

                //左上角删除图标
                hl = this.left + this.width - this.deleteBitmap.getWidth() / 2;
                ht = this.top + this.height - this.deleteBitmap.getHeight() / 2;
                hr = this.left + this.width + this.deleteBitmap.getWidth() / 2;
                hb = this.top + this.height + this.deleteBitmap.getHeight() / 2;
                rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
                ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.deleteBitmap, rs, ds, null);

            } else if (rate == PutyConst.RATE_90) {
                float hl = this.left + this.width / 2 - this.puvBitmap.getWidth() / 2;
                float ht = this.top + this.height - this.puvBitmap.getHeight() / 2;
                float hr = this.left + this.width / 2 + this.puvBitmap.getWidth() / 2;
                float hb = this.top + this.height + this.puvBitmap.getHeight() / 2;
                Rect rs = new Rect(0, 0, this.puvBitmap.getWidth(), this.puvBitmap.getHeight());
                RectF ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.puvBitmap, rs, ds, null);

                //左上角删除图标
                hl = this.left + this.width - this.deleteBitmap.getWidth() / 2;
                ht = this.top - this.deleteBitmap.getHeight() / 2;
                hr = this.left + this.width + this.deleteBitmap.getWidth() / 2;
                hb = this.top + this.deleteBitmap.getHeight() / 2;
                rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
                ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.deleteBitmap, rs, ds, null);

            } else {
                float hl = this.left + this.width / 2 - this.puvBitmap.getWidth() / 2;
                float ht = this.top - this.puvBitmap.getHeight() / 2;
                float hr = this.left + this.width / 2 + this.puvBitmap.getWidth() / 2;
                float hb = this.top + this.puvBitmap.getHeight() / 2;
                Rect rs = new Rect(0, 0, this.puvBitmap.getWidth(), this.puvBitmap.getHeight());
                RectF ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.puvBitmap, rs, ds, null);

                //左上角删除图标
                hl = this.left - this.deleteBitmap.getWidth() / 2;
                ht = this.top + this.height - this.deleteBitmap.getHeight() / 2;
                hr = this.left + this.deleteBitmap.getWidth() / 2;
                hb = this.top + this.height + this.deleteBitmap.getHeight() / 2;
                rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
                ds = new RectF(hl, ht, hr, hb);
                canvas.drawBitmap(this.deleteBitmap, rs, ds, null);
            }
        }
    }

    private char[] getKeyChar(String str) {
        char[] keys = new char[str.length()];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = str.charAt(i);
        }
        return keys;
    }

    /**
     * 判断元素是否缩放
     */
    @Override
    public Element isscale(float x, float y) {
        //文本
        RectF rectF;
        float hl, ht, hr, hb;
        if (this.rate == PutyConst.RATE_0) {
            hl = this.left + this.width - scaleImageWidth / 2;
            ht = this.top + this.height / 2 - scaleImageWidth / 2;
            hr = this.left + this.width + scaleImageWidth / 2;
            hb = this.top + this.height / 2 + scaleImageWidth / 2;
        } else if (this.rate == PutyConst.RATE_180) {
            hl = this.left - scaleImageWidth / 2;
            ht = this.top + this.height / 2 - scaleImageWidth / 2;
            hr = this.left + scaleImageWidth / 2;
            hb = this.top + this.height / 2 + scaleImageWidth / 2;
        } else if (rate == PutyConst.RATE_90) {
            hl = this.left + this.width / 2 - scaleImageWidth / 2;
            ht = this.top + this.height - scaleImageWidth / 2;
            hr = this.left + this.width / 2 + scaleImageWidth / 2;
            hb = this.top + this.height + scaleImageWidth / 2;
        } else {
            hl = this.left + this.width / 2 - scaleImageWidth / 2;
            ht = this.top - scaleImageWidth / 2;
            hr = this.left + this.width / 2 + scaleImageWidth / 2;
            hb = this.top + scaleImageWidth / 2;
        }

        rectF = new RectF(hl, ht, hr, hb);

        if ((x >= rectF.left && x <= rectF.right) && (y >= rectF.top && y <= rectF.bottom)) {
            return this;
        }
        return null;
    }

    /**
     * 是否删除
     */
    public BaseElement isDelete(float x, float y) {
        //二维码 判断是否点击缩放区域 右下角
        if ((x >= this.left - this.scaleImageWidth / 2 &&
                x <= this.left + this.scaleImageWidth / 2)
                && (y >= this.top - this.scaleImageWidth / 2 &&
                y <= this.top + this.scaleImageWidth / 2)) {
            return this;
        }
        return null;
    }

    /**
     * 复制对象
     *
     * @param element 原始对象
     * @param textElement 新的对象
     * @return 新的对象
     */
    public TextElement clone(Element element, TextElement textElement) {
        TextElement txt = (TextElement) element;
        textElement.inputMode = element.inputMode; // 数据类型
        textElement.textCellSpace = element.textCellSpace; // 字间距
        textElement.dataSourceColIndex = element.dataSourceColIndex; // 数据列号
        textElement.dataSourceColName = element.dataSourceColName; // 数据列名
        textElement.textMode = element.textMode; // 居中 靠左 靠右 拉伸
        textElement.top = element.top; // 元素顶部距离
        textElement.left = element.left; // 元素左侧距离
        textElement.scale = element.scale; // 缩放比例
        textElement.autoBreak = txt.autoBreak; // 自动换行
        textElement.textRowMode = txt.textRowMode; // 行模式
        textElement.textRowSpace = txt.textRowSpace; // 行间距
        textElement.familyName = txt.familyName; // 字体名称
        textElement.typeface = txt.typeface; // 字体类型
        textElement.fontBlod = txt.fontBlod; // 加粗
        textElement.fontUnderline = txt.fontUnderline; // 下划线
        textElement.fontDelete = txt.fontDelete; // 删除线
        textElement.fontIndex = txt.fontIndex; // 字体大小
        textElement.rate = txt.rate; // 旋转角度
        textElement.fontItalic = txt.fontItalic; // 斜体 一维码 。文本
        textElement.isPrinter = txt.isPrinter; // 0 不是打印对象 1打印
        textElement.ddStep = txt.ddStep; // 递变数据 增长步长
        textElement.isAntiWhite = txt.isAntiWhite;//反白
        textElement.foreground=txt.foreground;//文本反白
        //----------------------------另存文本换行-------------------------------
        textElement.width = element.width;
        textElement.height = element.height;
        //-----------------------------------------------------------

        if (SERIES_TYPE == INDUSTRY_SERIES) {
            textElement.isShow = element.isShow; // 是否显示
            textElement.isMirror = txt.isMirror; // 镜像
            textElement.type = txt.type; // 元素类型
            textElement.isblack = txt.isblack; // 黑白显示
            // 渲染时的缩放比例，解决标签镜像时，元素和镜像部分的线条虚线框显示不一致的bug
            textElement.renderScale = element.renderScale;
            // 缩放图标宽
            textElement.scaleImageWidth = element.scaleImageWidth;
        }

        return textElement;
    }

}